define(["baseAsset", "webgl", "assetSyst"], function(baseAsset, webgl, assetSyst) {
	return {
		create : function(path) {
		
			var _lineHeight = 0;
			var _glyph = [];
			var _toProcess = 0;
			var _callback, _params;
			var _images = [];
			
			function _doneProcessing() {
				if(--_toProcess <= 0) {
					asset.glyphs = _glyph;
					asset.images = _images;
					
					this.loadState 	= baseAsset.loadState.LOADED;
					_callback(asset, _params);
					_callback = _params = null;
				}
			}
			
			function _loadImage(xml) {
				++_toProcess;
				$(xml).find('page').each(function() {
					++_toProcess;
					var id 		= +$(this).attr('id');
					var file 	= $(this).attr('file');
					_images[id] = assetSyst.get(file, _doneProcessing);
				});
				_doneProcessing();
			}
			
			function _processGlyphs(xml) {
				//Say I'm processing
				++_toProcess;
			
				//save common
				_lineHeight = +$(xml).find('common').attr('lineHeight');
				var w = +$(xml).find('common').attr('scaleW');
				var h = +$(xml).find('common').attr('scaleH');
				
				//read each char
				$(xml).find('char').each(function() {
					var id = +$(this).attr('id');
					var glyph = {
						u 	: 	+$(this).attr('x')/w,
						u2 	: 	(+$(this).attr('x') + +$(this).attr('width'))/w,
						v 	:	+$(this).attr('y')/h,
						v2 	: 	(+$(this).attr('y') + +$(this).attr('height'))/h,
						x	: 	+$(this).attr('x'),
						y	: 	+$(this).attr('y'),
						w	:	+$(this).attr('width'),
						h	:	+$(this).attr('height'),
						xoff:	+$(this).attr('xoffset'),
						yoff:	+$(this).attr('yoffset'),
						xadv:	+$(this).attr('xadvance'),
					}
					_glyph[id] = glyph;
				});
				
				//done
				_doneProcessing();
			}
		
			function _processFntXml(xml, txtStatus, jqXHR) {
				++_toProcess;
				_loadImage(xml);
				_processGlyphs(xml);
				_doneProcessing();
			}
		
			var asset = baseAsset.create(path);
			
			asset.load = function(callback, params) {
				this.loadState 	= baseAsset.loadState.LOADING;
				var jqxhr 		= $.get(this.getPath(), null, _processFntXml, "xml");
				_callback 		= callback;
				_params 		= params;
			};
			
			asset.unload = function() {
				
			};
			
			return asset;
		} 
	};
});